﻿using System.Diagnostics;
using Domain.Repository;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using UI.Models;

namespace UI.Controllers
{
    public class HomeController : Controller
    {
        private readonly ITaskRepository _taskRepository;
        private readonly ILogger<HomeController> _logger;
        private readonly string _currentUser;
        private readonly MainMenu[] _mainMenu;

        public HomeController(
            IHttpContextAccessor httpContextAccessor,
            ITaskRepository taskRepository,
            ILogger<HomeController> logger)
        {
            _taskRepository = taskRepository ?? throw new ArgumentNullException(nameof(taskRepository));
            _logger = logger;
            _currentUser = httpContextAccessor.HttpContext.User.Identity?.Name;

            //  TODO: 从user.claims中获取
            _mainMenu = new MainMenu[]
            {
                new MainMenu{Name = "中台预审", Display = "中台预审"},
                new MainMenu{Name = "交易审批", Display = "交易审批", Code = "OTCTRADE"},
                new MainMenu{Name = "财务审批", Display = "财务审批"},
                new MainMenu{Name = "清算审批", Display = "清算审批"},
            };

        }

        [Authorize]
        public IActionResult Index()
        {

            ViewData["aGroupCode"] = Request.Query["aGroupCode"].FirstOrDefault() ?? "0";

            return View(_mainMenu);
        }

        public IActionResult ApproveTree(int tid)
        {
            ViewData["TID"] = tid;
            return View();
        }

        public IActionResult TraceLog(int tid)
        {
            ViewData["TID"] = tid;
            return View();
        }

        public IActionResult Limit(int tid)
        {
            ViewData["TID"] = tid;
            return View();
        }

        public async Task<IActionResult> Target(string tid)
        {
            var rlt = await _taskRepository.MobileSingleTradeTarget(new Domain.Repository.RequestModels.BaseRequestModel
            {
                aTaskId = tid,
                aUserCode = _currentUser
            });
            return View(rlt);
        }

        public async Task<IActionResult> Detail(string oid)
        {
            var rlt = await _taskRepository.MobileSingleTradeDetailNew(new Domain.Repository.RequestModels.BaseRequestModel
            {
                aSysOrdId = oid,
                aUserCode = _currentUser
            });

            return View(rlt);
        }

        public IActionResult GroupIndex()
        {
            return View();
        }

        [AllowAnonymous]
        public IActionResult Login()
        {
            return View();
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}